﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace RVBike
{
    public class SaveObject
    {
        public List<SaveState> States { get; set; }

        public SaveObject()
        {
            States = new List<SaveState>();
        }
    }

    class SaveManager : Singleton<SaveManager>
    {
        private const string SAVE_FILE_NAME = "SAVE.dat";
        private SaveObject _saves = null;

        public SaveManager()
        {
            try
            {
                Load();
            }
            catch (Exception e)
            {
                Logger.Instance().Error(String.Format("Failed to load save file : {0}", e));
                throw e;
            }
        }

        private void Load()
        {
            Logger.Instance().Info("Start loading save file ...");
            string saveFile = PathUtility.Instance().SavesDir + SAVE_FILE_NAME;
            if (!File.Exists(saveFile))
            {
                Logger.Instance().Info("No save file, create default save file");

                // Create an empty save file if not exist
                CreateDefaultSaveFile(saveFile);
            }

            // Load from file
            using (StreamReader file = new StreamReader(saveFile))
            {
                string saveData = file.ReadToEnd();
                _saves = JsonConvert.DeserializeObject<SaveObject>(saveData);
            }
        }

        private void Save()
        {
            Logger.Instance().Info("Start loading save file ...");
            string saveFile = PathUtility.Instance().SavesDir + SAVE_FILE_NAME;

            using (StreamWriter file = new StreamWriter(saveFile))
            {
                string saveData = JsonConvert.SerializeObject(_saves, Formatting.Indented);
                file.Write(saveData);
            }
        }

        private void CreateDefaultSaveFile(string path)
        {
            SaveObject so = new SaveObject();
            using (StreamWriter file = new StreamWriter(path))
            {
                string saveData = JsonConvert.SerializeObject(so);
                file.Write(saveData);
            }
        }

        public int NumSaves()
        {
            if (_saves != null)
            {
                return _saves.States.Count;
            }

            return 0;
        }

        public SaveState GetSave(int index)
        {
            if (_saves == null ||
                _saves.States.Count <= index)
            {
                return null;
            }

            return _saves.States[index];
        }

        public bool RemoveSave(int index)
        {
            if (_saves == null ||
                _saves.States.Count <= index)
            {
                return false;
            }

            _saves.States.RemoveAt(index);
            Save();

            return true;
        }

        public bool AddSave(SaveState ss)
        {
            if (ss == null)
            {
                return false;
            }

            _saves.States.Add(ss);
            Save();

            return true;
        }
    }
}
